/*
 * util.h
 *
 *  Created on: Aug 12, 2014
 *      Author: burek
 */

#ifndef UTIL_H_
#define UTIL_H_

#include "types/integers.h"
#include "glm/glm.hpp"

const float PIf = 3.14159265359f;
const float DOUBLE_PIf = 6.28318530718f;
const float HALF_PIf = 1.57079632679f;

void R4_rerandomize();
bool R4_chance(int percent);
int R4_randRangeI(int min, int max);
float R4_randRangeF(float min, float max);
float R4_randRangeFS(float min, float max);

void R4_endianSwap(uint16_t & x);
void R4_endianSwap(uint32_t & x);
void R4_endianSwap(uint64_t & x);
void R4_endianSwap(int16_t & x);
void R4_endianSwap(int32_t & x);
void R4_endianSwap(int64_t & x);

uint16_t R4_endianSwapR(uint16_t x);
uint32_t R4_endianSwapR(uint32_t x);
uint64_t R4_endianSwapR(uint64_t x);
int16_t R4_endianSwapR(int16_t x);
int32_t R4_endianSwapR(int32_t x);
int64_t R4_endianSwapR(int64_t x);

glm::vec3 R4_colorFromRGB(int r, int g, int b);
glm::vec4 R4_colorFromRGBA(int r, int g, int b, int a);
glm::vec3 R4_colorFromRGB(uint32_t rgb);
glm::vec4 R4_colorFromRGBA(uint32_t rgba);
glm::vec3 R4_colorFromHexRGB(const char * hex);
glm::vec4 R4_colorFromHexRGBA(const char * hex);

uint32_t R4_rgbFromVec(const glm::vec3 & v);
uint32_t R4_rgbaFromVec(const glm::vec4 & v);

template <typename T> void R4_swap(T & a, T & b)
{
	T c = a;
	a = b;
	b = c;
}

template <typename T> inline void clampMax(T &num, const T max)
{
	num = (num > max) ? max : num;
}

template <typename T> inline void clampMin(T &num, const T min)
{
	num = (num < min) ? min : num;
}

template <typename T> inline void clamp(T &num, const T min, const T max)
{
	num = (num > max) ? max : num;
	num = (num < min) ? min : num;
}

template <typename T> T clampMaxRet(const T num, const T max)
{
	return (num > max) ? max : num;
}

template <typename T> T clampMinRet(const T num, const T min)
{
	return (num < min) ? min : num;
}

template <typename T> T clampRet(const T num, const T min, const T max)
{
	T x = (num > max) ? max : num;
	return (x < min) ? min : x;
}

uint32_t R4_calcSimpleHash(const char * txt);

bool isPOT(uint32_t n);
uint32_t nextPOT(uint32_t n);

#endif /* UTIL_H_ */
